*12-01-25_Moum2012build.do

***This document contains the code to construct the variables for the following work:
*Dafoe, Allan and Bruce Russett. 2012. Democracy and Capitalism: Interwoven Strands of the Liberal Peace. in Eds. Nils Petter Gleditsch and Gerald Schneider. The Capitalist Peace. 

*This code is based almost entirely on the code and data for the chapter to which it responds: 

*Mousseau, Michael, Omer F. Orsue, Jameson L. Ungerer, and Demet Y. Mousseau. 2012. Capitalism and Peace: It's Keynes, not Hayek. in Eds. Nils Petter Gleditsch and Gerald Schneider. The Capitalist Peace. 



*****************************************************************************
**************STEP 1: GENERATING VARIABLES***********************************
*****************************************************************************


********************************************
****Generating the Independent Variables****
********************************************


use "Step1 reduced.dta", clear

su year

replace 	tpop_1		=. if tpop_1	 	==-9
replace 	tpop_2		=. if tpop_2	 	==-9
replace 	energy_1	=. if energy_1	==-9
replace 	energy_2	=. if energy_2	==-9
replace 	cap_1		=. if cap_1	 	==-9
replace 	cap_2		=. if cap_2	 	==-9
replace 	polity21	=. if polity21	==-99
replace 	polity22	=. if polity22	==-99
replace 	capopenl	=. if capopenl	==-9
replace 	pub1	=. if pub1		==-9
replace 	pub2	=. if pub2		==-9
replace     distance=. if distance==-9
replace     region1=. if region1==-9
replace     region2=. if region2==-9


g cpl = cap_1 if cap_1 < cap_2 & cap_1~=. & cap_2~=.
replace cpl = cap_2 if cap_2 <= cap_1 & cap_1~=. & cap_2~=.
g cph = cap_1 if cap_1 > cap_2 & cap_1~=. & cap_2~=.
replace cph = cap_2 if cap_2 >= cap_1 & cap_1~=. & cap_2~=.
g lncprt=ln(cph/cpl) if cph~=. & cpl~=.
drop cap_1 cap_2 
label variable lncprt "Relative capability"

gen majpow=0
replace majpow=1 if majpow1==1 | majpow2==1
replace majpow=. if majpow1==. | majpow2==.
label variable majpow "Major power"

g cntgd=0 
replace cntgd = 1 if contig == 1
label variable cntgd "Contiguity"

g dist=ln(distance) if distance~=.
label variable dist "LogDistance"

g CIEl= CIE1 if CIE1~=. & CIE2~=.
replace CIEl= CIE2 if CIE1> CIE2 & CIE1~=. & CIE2~=.
label variable CIEl "CIEL"

g CIEh= max(CIE1,CIE2) if CIE1~=. & CIE2~=.
label variable CIEh "CIEH"


g dml=polity21 if polity21~=. & polity22~=.
replace dml= polity22 if polity21> polity22 & polity21~=. & polity22~=.
label variable dml "DemocracyL"
g dmh=polity21 if polity21~=. & polity22~=.
replace dmh= polity22 if polity21< polity22 & polity21~=. & polity22~=.
g dmdist = dmh-dml if dmh~=. & dml~=.
drop polity21 polity22 
label variable dmdist "Regime Difference"

g bdm=0
replace bdm=1 if dml >=7
replace bdm=. if dml==.
label variable bdm "DemocracyBinary6"

g dm10=0
replace dm10=1 if dml>=10
replace dm10=. if dml==.
label variable dm10 "DemocracyBinary10"

gen dml2=dml+10
gen dmlsq=dml2^2
label variable dmlsq "DemocracyL^2"

g edv1= energy_1/ tpop_1
g edv2= energy_2/ tpop_2
g edvl= ln(1+edv1) if edv1 ~=. & edv2 ~=.
replace edvl= ln(1+edv2) if edv2<edv1 & edv1 ~=. & edv2 ~=.
label variable edvl "WealthL"

g dpl=dp1*100 if dp1~=. & dp2~=.
replace dpl= dp2*100 if dp1> dp2 & dp1~=. & dp2~=.
drop dp1 dp2
label variable dpl "TradeL"

g pubh=pub1 if pub1~=. & pub2~=.
replace pubh= pub2 if pub2> pub1 & pub1~=. & pub2~=.
label variable pubh "PublicH"

joinby dyadid year using "ICB data.dta", unmatched(both)
tab _merge
drop if _merge==2
*We have deleted 230 observations because our dataset starts from 1943 (this is not the analysis start year).
*As a result, now we have 803 ICB cases-230=573 cases of ICBs
drop _merge

sort dyadid year
xtset dyadid year
replace crisis=0 if crisis==.
gen crisis1= F1.crisis
*This command shifts crisis forward one year. 
*(19455 missing values generated)
*This means we are left with 539 ICB cases. 
label variable crisis1 "Crisis t+1"
drop if year<1947
*We now have 523 ICB cases.
btscs crisis1 year dyadid, g(peaceyrs) nspline(3)
label var _spline1 "Spline 1"
label var _spline2 "Spline 2"
label var _spline3 "Spline 3"
drop if year<1960
*We now have 394 cases of ICBs for our crisis t+1 variable. 
*Therefore, for the period of 1960-2000, one can only have 394 cases of ICB onsets.

label variable numstate "Number of states"

sort dyadid
gen capopenl2=capopenl
replace capopenl2=0 if (capopenl==. & year>=1966 & year<1993)
labe variable capopenl  "CAPOPENL (Gartzke 2007)"
labe variable capopenl2 "CAPOPENL (Gartzke & Hewitt 2010)"
by dyadid: ipolate capopenl year, gen(capopenl_ipol)
gen capopenl_ipol2=capopenl_ipol
replace capopenl_ipol2=0 if (capopenl_ipol==. & year>=1966 & year<=1992)
labe variable capopenl_ipol2 "Capital OpennessL"

gen CIEdiff=CIEh-CIEl

gen CIElDml=CIEl*dml
gen CIElbdm=CIEl*bdm
gen CIEldm10=CIEl*dm10
gen CIEldmlsq=CIEl*dmlsq

order dyadid year ccode1 ccode2 year crisis1 CIEl CIEh dml bdm dm10 dmlsq dmdist edvl dpl capopenl capopenl2 capopenl_ipol2 pubh lncprt  majpow cntgd dist  numstate  peaceyrs _spline1 _spline2 _spline3

*keep dyadid ccode1 ccode2 year crisis1 CIEl dml bdm dm10 dmlsq dmdist edvl dpl capopenl capopenl2 capopenl_ipol2 pubh lncprt  majpow cntgd dist  numstate  peaceyrs _spline1 _spline2 _spline3
